<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import PlanItemList from './PlanItemList.vue'
import dayjs from 'dayjs'
import QcPlanReportModal from './modules/QcPlanReportModal.vue'
import { getAction, postAction } from '@api/manage'

export default {
  name: 'SemiFinishedProductInspectionList',
  components: { QcPlanReportModal, PlanItemList },
  mixins: [JeecgListMixin],
  data() {
    return {
      columns: [
        {
          title: '计划单号',
          align: 'center',
          dataIndex: 'code'
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'workshopCode_dictText'
        },
        {
          title: '执行日期',
          align: 'center',
          dataIndex: 'workTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText'
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        // isChanged
        {
          title: '是否变更计划',
          align: 'center',
          dataIndex: 'isUpdate_dictText'
        },
        {
          title: '检验次数',
          align: 'center',
          dataIndex: 'checkCount'
        }
      ],
      url: {
        list: '/pro/tslProWorkshopPlan/list',
        count: '/qms/inspection/plan/report/semi-finished-product/queryCount'
      },
      queryParam: {
        createTimeBegin: dayjs().subtract(7, 'day').format('YYYY-MM-DD'),
        createTimeEnd: dayjs().format('YYYY-MM-DD')
      },
      planItemMainId: ''
    }
  },
  methods: {
    loadData() {
      getAction(this.url.list).then(res => {
        let { result } = res
        if (result && result.records) {
          this.dataSource = result.records
          this.dataSourceBack = this.dataSource
          if (result.total) {
            this.ipagination.total = result.total
          } else {
            this.ipagination.total = 0
          }
          const codes = result.records.map(item => item.code)
          this.fetchCountData(codes)
        }
      })
    },
    fetchCountData(codes) {
      postAction(this.url.count, codes).then(res => {
        let { result } = res
        if (result) {
          this.dataSource.forEach(item => {
            const count = result[item.code]
            this.$set(item, 'checkCount', count)
          })
        }
      })
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            // this.onSelectChange(record.id.split(','), [record])
            this.planItemMainId = record.id
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      // this.planItemMainId = selectionRows[0]['id']
    },
    selectValidate() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请先选中一条记录')
        return false
      }
      if (this.selectionRows.length > 1) {
        this.$message.error('最多只能选择一条记录')
        return false
      }
      return true
    },
    handleAdd: function() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请选中至少一条记录')
        return
      }
      this.$refs.modalForm.add(this.selectionRows)
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.reportType = 'semi-finished-product'
    },
  }
}
</script>

<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="制单日期">
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择开始时间"
                class="query-group-cust"
                v-model="queryParam.createTimeBegin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD"
                placeholder="请选择结束时间"
                class="query-group-cust"
                v-model="queryParam.createTimeEnd"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap1"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'checkbox'}"
        :customRow="clickThenSelect"
        @change="handleTableChange">
      </a-table>
    </div>
    <plan-item-list :mainId="planItemMainId" />

    <qc-plan-report-modal ref="modalForm" @ok="modalFormOk" />
  </a-card>
</template>

<style scoped>

</style>